代写C++代码 代做C++程序 C++辅导 C++教学

C++网络家教 远程写代码 在线Debug 讲解答疑 不是中介,本人直接写

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

a simple calculator

In this assignment you are to prepare a C/Objective-C program that will act as a simple
calculator. The calculator will be run from the command line and will only work with integer
numbers and the following arithmetic operators + - x / %. The % operator is the
modulus operator, not the percentage.
For example, if the C/Objective-C program is compiled to calc, the following demonstrates
how it will work
./calc 3 + 5 - 7
1
In the command line, the arguments are a repeated sequence in the form
number operator
and ending in a
number
Hitting the enter key will cause the program to evaluate the arguments and print the result.
In this case 1.
The program must follow the usual laws of arithmetic which says
1. The x / and % operators must all be evaluated before the + and operators.
2. Operators must be evaluated from left to right.
For example, using Rule 1
2 + 4 x 3 – 6
becomes
2 + 12 – 6
which results in
8
Assignment 1 – Autumn 2016 Page 2 of 6
If we did not use Rule 1 then 2 + 4 x 3 – 6 would become 6 x 3 – 6 and then 18 – 6 and finally
12. This is an incorrect result.
If we do not use Rule 2 then the following illustrates how it can go wrong
4 x 5 % 2
Going from left to right we evaluate the x first, which reduces the expression to 20 % 2
which becomes 0. If we evaluated the % first then the expression would reduce to 4 x 1
which becomes 4. This is an incorrect result.
Remember, we are using integer mathematics when doing our calculations, so we get
integer results when doing division. For example
./calc 20 / 3
6
Also note that we can use the unary + and 浏览:411

分页: 首页 1 尾页